package com.servlet.rootoperator;

import com.Impl.BookCRUD;
import com.alibaba.fastjson.JSON;
import com.entity.Book;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "PaginationServlet", value = "/PaginationServlet")
public class PaginationServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageSizeStr = request.getParameter("pageSize");
        String startIndexStr = request.getParameter("startIndex");
//        处理乱码问题
        response.setContentType("text/html;charset=utf-8");
        System.out.println(startIndexStr+"   "+pageSizeStr);
        int startIndex = Integer.parseInt(startIndexStr);
        int pageSize = Integer.parseInt(pageSizeStr);
        BookCRUD bookCRUD = new BookCRUD();
        List<Book> books = bookCRUD.queryBooksByPage(startIndex, pageSize);
        String s = JSON.toJSONString(books);
        response.getWriter().write(s);
    }
}
